﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DataAccess;
using System.Data;
using YoutubeAccess;
using BusinessLogic;
using System.Data.Common;
using BusinessLogic.Data;

namespace TestProject1
{
    [TestClass]
    public class DataAccessTests
    {
        [TestMethod ]
        public void CheckDbConnectivity()
        {
            DataSet ds = DbHelper.Instance.GetExistingWebinars(null, 0, 10);
        }

        [TestMethod]
        public void CheckLoginAndRegistration()
        {
            using (DbConnection connection = DbHelper.Instance.CreateConnection ())
            {
                DbTransaction trans = connection.BeginTransaction(System.Data.IsolationLevel.Snapshot);
                try
                {
                    string name = "sagi.test";
                    string pwd = "1234567890";

                    int id = DbHelper.Instance.RegisterUser(trans, name, pwd, true);
                    Assert.IsFalse(id <= 0);

                    DataSet ds = DbHelper.Instance.Login(trans, name, pwd);

                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);
                    
                    User usr =  new User();
                    usr.Load(ds.Tables[0].Rows[0]);

                    Assert.AreNotEqual<bool> (usr.IsAdmin, false);
                }
                finally
                {
                    trans.Rollback();
                    connection.Close();
                }
            }

        }

        [TestMethod]
        public void CheckYoutubeConnectivity()
        {
            Webinar[] res =  new GoogleHelper().SearchWebinarSite(new DateTime (2013, 1, 1));
            Assert.AreNotEqual<int>(res.Length, 0);
            
            Webinar[] res1 = new GoogleHelper().SearchWebinarSite(null);
            Assert.AreNotEqual<int>(res1.Length, 0);

            Webinar[] res2 = new GoogleHelper().SearchWebinarSite(DateTime.MaxValue);
            Assert.AreEqual<int>(res2.Length, 0);
        }

        [TestMethod]
        public void TestAddWebinar()
        {
            using (DbConnection connection = DbHelper.Instance.CreateConnection())
            {
                DbTransaction trans = connection.BeginTransaction(System.Data.IsolationLevel.Snapshot);
                try
                {
                    string uid = Guid.NewGuid().ToString();
                    DateTime dt = DateTime.Today.ToUniversalTime();
                    string uri = "http://localhost/MySite";
                    string name = "test Name";
                    string desc = "test Desc";

                    DataSet ds = DbHelper.Instance.AddWebinar(trans, uid, name, dt, uri, desc);
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Webinar webinar = new Webinar(ds.Tables[0].Rows[0]);
                    Assert.IsTrue(webinar.ID > 0);
                    Assert.IsTrue(webinar.UID == uid);
                    Assert.IsTrue(webinar.Scheduled == dt);
                    Assert.IsTrue(webinar.URI == uri);
                    Assert.IsTrue(webinar.Name == name);
                    Assert.IsTrue(webinar.Description == desc);
                }
                finally
                {
                    trans.Rollback();
                    connection.Close();
                }
            }
        }

        [TestMethod]
        public void TestAddQuestion()
        {
            using (DbConnection connection = DbHelper.Instance.CreateConnection())
            {
                DbTransaction trans = connection.BeginTransaction(System.Data.IsolationLevel.Snapshot);
                try
                {
                    string name = "sagi.test";
                    string pwd = "1234567890";

                    int userId = DbHelper.Instance.RegisterUser(trans, name, pwd, true);
                    Assert.IsFalse(userId <= 0);

                    DataSet ds = DbHelper.Instance.AddWebinar(trans, Guid.NewGuid().ToString(), "test", DateTime.Now, "http://localhost/MySite", "test");
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Webinar webinar = new Webinar(ds.Tables[0].Rows[0]);
                    Assert.IsTrue(webinar.ID > 0);

                    Question que = new Question
                    {
                        Active = true,
                        CreationDate = DateTime.Now,
                        UserID = userId,
                        UserName = "mot initialized",
                        Votes = 0,
                        Content = "test content"
                    };

                    ds = DbHelper.Instance.AddQuestion(trans, webinar.ID, que.UserID, que.Content);
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                }
                finally
                {
                    trans.Rollback();
                    connection.Close();
                }
            }
        }

        [TestMethod]
        public void TestAddComment()
        {
            using (DbConnection connection = DbHelper.Instance.CreateConnection())
            {
                DbTransaction trans = connection.BeginTransaction(System.Data.IsolationLevel.Snapshot);
                try
                {
                    string name = "sagi.test";
                    string pwd = "1234567890";

                    int userId = DbHelper.Instance.RegisterUser(trans, name, pwd, true);
                    Assert.IsFalse(userId <= 0);

                    DataSet ds = DbHelper.Instance.AddWebinar(trans, Guid.NewGuid().ToString(), "test", DateTime.Now, "http://localhost/MySite", "test");
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Webinar webinar = new Webinar(ds.Tables[0].Rows[0]);
                    Assert.IsTrue(webinar.ID > 0);

                    Question que = new Question
                    {
                        UserID = userId,
                        Content = "test content"
                    };

                    ds = DbHelper.Instance.AddQuestion(trans, webinar.ID, que.UserID, que.Content);
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Question qu = new Question(userId, ds.Tables[0].Rows[0], null);

                    Comment comment = new Comment
                    {
                        Content = "סתם תגובה",
                        UserID = userId
                    };

                    int commentId = DbHelper.Instance.AddComment(trans, qu.ID, que.UserID, que.Content);
                    Assert.IsTrue(qu.ID > 0);
                }
                finally
                {
                    trans.Rollback();
                    connection.Close();
                }
            }
        }

        [TestMethod]
        public void TestVote()
        {
            using (DbConnection connection = DbHelper.Instance.CreateConnection())
            {
                DbTransaction trans = connection.BeginTransaction(System.Data.IsolationLevel.Snapshot);
                try
                {
                    string name = "sagi.test";
                    string pwd = "1234567890";

                    int userId = DbHelper.Instance.RegisterUser(trans, name, pwd, true);
                    Assert.IsFalse(userId <= 0);

                    DataSet ds = DbHelper.Instance.AddWebinar(trans, Guid.NewGuid().ToString(), "test", DateTime.Now, "http://localhost/MySite", "test");
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Webinar webinar = new Webinar(ds.Tables[0].Rows[0]);
                    Assert.IsTrue(webinar.ID > 0);

                    Question que = new Question
                    {
                        UserID = userId,
                        Content = "test content"
                    };

                    ds = DbHelper.Instance.AddQuestion(trans, webinar.ID, que.UserID, que.Content);
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Question qu = new Question(userId, ds.Tables[0].Rows[0], null);

                    Assert.IsTrue(DbHelper.Instance.Vote(trans, qu.ID, que.UserID, true));

                    Assert.IsFalse(DbHelper.Instance.Vote(trans, qu.ID, que.UserID, true));
                }
                finally
                {
                    trans.Rollback();
                    connection.Close();
                }
            }
        }

        [TestMethod]
        public void TestMarkAsAnswered_Admin()
        {
            using (DbConnection connection = DbHelper.Instance.CreateConnection())
            {
                DbTransaction trans = connection.BeginTransaction(System.Data.IsolationLevel.Snapshot);
                try
                {
                    string name = "sagi.test";
                    string pwd = "1234567890";

                    int userId = DbHelper.Instance.RegisterUser(trans, name, pwd, true);
                    Assert.IsFalse(userId <= 0);

                    DataSet ds = DbHelper.Instance.AddWebinar(trans, Guid.NewGuid().ToString(), "test", DateTime.Now, "http://localhost/MySite", "test");
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Webinar webinar = new Webinar(ds.Tables[0].Rows[0]);
                    Assert.IsTrue(webinar.ID > 0);

                    Question que = new Question
                    {
                        UserID = userId,
                        Content = "test content"
                    };

                    ds = DbHelper.Instance.AddQuestion(trans, webinar.ID, que.UserID, que.Content);
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Question qu = new Question(userId, ds.Tables[0].Rows[0], null);

                    Assert.IsTrue(DbHelper.Instance.MarkAsAnswered(trans, qu.ID, que.UserID));
                }
                finally
                {
                    trans.Rollback();
                    connection.Close();
                }
            }
        }

        [TestMethod]
        public void TestMarkAsAnswered_NotAdmin()
        {
            using (DbConnection connection = DbHelper.Instance.CreateConnection())
            {
                DbTransaction trans = connection.BeginTransaction(System.Data.IsolationLevel.Snapshot);
                try
                {
                    string name = "sagi.test";
                    string pwd = "1234567890";

                    string name2 = "sagi.test2";

                    int userId = DbHelper.Instance.RegisterUser(trans, name, pwd, false);
                    Assert.IsFalse(userId <= 0);

                    int user2Id = DbHelper.Instance.RegisterUser(trans, name2, pwd, false);
                    Assert.IsFalse(user2Id <= 0);

                    DataSet ds = DbHelper.Instance.AddWebinar(trans, Guid.NewGuid().ToString(), "test", DateTime.Now, "http://localhost/MySite", "test");
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Webinar webinar = new Webinar(ds.Tables[0].Rows[0]);
                    Assert.IsTrue(webinar.ID > 0);

                    Question que = new Question
                    {
                        UserID = userId,
                        Content = "test content"
                    };

                    ds = DbHelper.Instance.AddQuestion(trans, webinar.ID, que.UserID, que.Content);
                    Assert.IsTrue(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0);

                    Question qu = new Question(userId, ds.Tables[0].Rows[0], null);


                    Assert.IsTrue(DbHelper.Instance.MarkAsAnswered(trans, qu.ID, userId));

                    Assert.IsFalse(DbHelper.Instance.MarkAsAnswered(trans, qu.ID, user2Id));
                }
                finally
                {
                    trans.Rollback();
                    connection.Close();
                }
            }
        }

    }
}
